#' -----------------------------------------------------------
# Constructive and Destructive Legislative Review: 
# The Government-Opposition Divide in Parliamentary Oversight
# Reproduction File
# Lion Behrens, Dominic Nyhuis, Thomas Gschwend
# November 2021
#' -----------------------------------------------------------

# check if packages are installed 
list.of.packages <- c("stringr", "qdapRegex", "ggplot2", "MASS", "readstata13",
                      "RCurl", "tikzDevice", "gridExtra", "stargazer",
                      "lme4", "dplyr", "rstudioapi", "logr")
new.packages <- list.of.packages[!(list.of.packages %in% 
                                     installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

library(stringr)  
library(qdapRegex)  
library(ggplot2)
library(MASS)  
library(readstata13)
library(RCurl)           
library(tikzDevice)     
library(gridExtra)       
library(stargazer)       
library(lme4)            
library(dplyr)           
library(rstudioapi)
library(logr)

setwd(dirname(getActiveDocumentContext()$path))
log_open()

# data
load("bawue_1416.RData")

# exclusion of four laws for which the legislative process was not finished 
# and which have not been assigned to a committee yet at the time of scraping
legistext <- legistext[-which(legistext$idDrucksache == "16/5113" |
                              legistext$idDrucksache == "16/5311" |
                              legistext$idDrucksache == "16/5421" |
                              legistext$idDrucksache == "16/5422"),]

#' -----------------------------------------
# 1. data preparation ----------------------
#' ----------------------------------------

  #' -------------------------------------------
  # 1.1 convert data to bill x party level -----
  #' -------------------------------------------
  
    # government bill identifier
    gov_wp14 <- which(substr(legistext$idDrucksache, 1, 2) == "14" &
                     (legistext$Landesregierung==1 | 
                      legistext$CDU==1 | 
                      legistext$FDP==1))
  
    gov_wp15 <- which(substr(legistext$idDrucksache, 1, 2) == "15" & 
                     (legistext$Landesregierung==1 | 
                      legistext$GRUENE==1 | 
                      legistext$SPD==1))
    
    gov_wp16 <- which(substr(legistext$idDrucksache, 1, 2) == "16" & 
                     (legistext$Landesregierung==1 | 
                      legistext$GRUENE==1 | 
                      legistext$CDU==1))
    
    # only keep government bills
    gov_legistext <- legistext[c(gov_wp14, gov_wp15, gov_wp16),]
    rownames(gov_legistext) <- 1:nrow(gov_legistext)
    
    # legislative period identifier
    wp14 <- which(substr(gov_legistext$idDrucksache, 1, 2) == "14")
    wp15 <- which(substr(gov_legistext$idDrucksache, 1, 2) == "15")
    wp16 <- which(substr(gov_legistext$idDrucksache, 1, 2) == "16" )
    
    # construct bill x party level
    billxparty <- rbind(gov_legistext[c(wp14, wp15),][rep(seq_len(nrow(gov_legistext[c(wp14, wp15),])), each=4),], 
                        gov_legistext[wp16,][rep(seq_len(nrow(gov_legistext[wp16,])), each=5),])
    rownames(billxparty) <- 1:nrow(billxparty)
    
    billxparty$fraktion <- c(rep(c("CDU", "SPD", "Gruene", "FDP/DVP"), length(wp14)), 
                             rep(c("CDU", "SPD", "Gruene", "FDP/DVP"), length(wp15)),
                             rep(c("CDU", "SPD", "Gruene", "FDP/DVP", "AfD"), length(wp16)))
    
    
    #' ----------------------------------------
    # 1.2 variables ---------------------------
    #' ----------------------------------------
    
      ### policy fields
      amendmentprop$cap1_field <- NA
      amendmentprop$cap2_field <- NA
      
      for (i in 1:nrow(amendmentprop)) {
        
        legis_id <- which(legistext$idDrucksache == amendmentprop$idDrucksacheLegis[i])
        amendmentprop$cap1_field[i] <- legistext$cap1_field[legis_id]
        amendmentprop$cap2_field[i] <- legistext$cap2_field[legis_id]
        
      }
    
      ### issue-ownership (GIP-measured)
      ownership <- read.dta13("gip_ownership.dta")
      ownership <- cbind(1, ownership)
      att <- attributes(ownership)
      ownership <- ownership[,c(1:73, 76, 74, 75, 77:145, 148, 146, 147)]
      
      # create empty dataframe to store summaries of perceived competence
      issues <- as.data.frame(matrix(NA, 
                                     ncol=24,
                                     nrow=10))
      colnames(issues) <- gsub(" [(]cducsu[)]", "",att$var.labels[5:28])
      colnames(issues)[9] <- "Equality"
      rownames(issues) <- c("CDU", "CDU_bw", "SPD", "SPD_bw", "Gruene", "Gruene_bw",
                            "FDP", "FDP_bw", "AfD", "AfD_bw")
      
      # CDU
      it <- 0
      for (col in 5:28) {
        it <- it + 1
        
        issues["CDU",it] <- 
          length(which(ownership[,col]=="1. Item checked")) / 
          (length(which(ownership[,col]=="0. Item not checked")) + length(which(ownership[,col]=="1. Item checked")))
        
        issues["CDU_bw",it] <- 
          length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")) / 
          (length(which(ownership[which(ownership$bawu==1),col]=="0. Item not checked")) + length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")))
        
      }
      
      # SPD
      it <- 0
      for (col in 29:52) {
        it <- it + 1
        
        issues["SPD",it] <- 
          length(which(ownership[,col]=="1. Item checked")) / 
          (length(which(ownership[,col]=="0. Item not checked")) + length(which(ownership[,col]=="1. Item checked")))
        
        issues["SPD_bw",it] <- 
          length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")) / 
          (length(which(ownership[which(ownership$bawu==1),col]=="0. Item not checked")) + length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")))
        
      }
      
      # FDP
      it <- 0
      for (col in 53:76) {
        it <- it + 1
        
        issues["FDP",it] <- 
          length(which(ownership[,col]=="1. Item checked")) / 
          (length(which(ownership[,col]=="0. Item not checked")) + length(which(ownership[,col]=="1. Item checked")))
        
        issues["FDP_bw",it] <- 
          length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")) / 
          (length(which(ownership[which(ownership$bawu==1),col]=="0. Item not checked")) + length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")))
        
      }
      
      # Gruene
      it <- 0
      for (col in 77:100) {
        it <- it + 1
        
        issues["Gruene",it] <- 
          length(which(ownership[,col]=="1. Item checked")) / 
          (length(which(ownership[,col]=="0. Item not checked")) + length(which(ownership[,col]=="1. Item checked")))
        
        issues["Gruene_bw",it] <- 
          length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")) / 
          (length(which(ownership[which(ownership$bawu==1),col]=="0. Item not checked")) + length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")))
        
      }
      
      # AfD
      it <- 0
      for (col in 125:148) {
        it <- it + 1
        
        issues["AfD",it] <- 
          length(which(ownership[,col]=="1. Item checked")) / 
          (length(which(ownership[,col]=="0. Item not checked")) + length(which(ownership[,col]=="1. Item checked")))
        
        issues["AfD_bw",it] <- 
          length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")) / 
          (length(which(ownership[which(ownership$bawu==1),col]=="0. Item not checked")) + length(which(ownership[which(ownership$bawu==1),col]=="1. Item checked")))
        
      }
      
        
        # categorical measure of perceived issue competence 
        # the two most prominent policy fields
        billxparty$gipc <- 0
        billxparty$gipc_bw <- 0
        
        # CDU: Banking, Finance, Domestic Commerce; Macroeconomy; Law, Crime and Family Issues
        # CDU_bw: Banking, Finance, Domestic Commerce; Macroeconomy; Law, Crime and Family Issues
        billxparty[which(billxparty$fraktion=="CDU" & 
                           (billxparty$cap1_field=="Banking, Finance and Domestic Commerce" |
                              billxparty$cap1_field=="Macroeconomy" |   
                              billxparty$cap1_field=="Law, Crime and Family Issues")),]$gipc <- 1
        billxparty[which(billxparty$fraktion=="CDU" & 
                           (billxparty$cap1_field=="Banking, Finance and Domestic Commerce" |
                              billxparty$cap1_field=="Macroeconomy" |   
                              billxparty$cap1_field=="Law, Crime and Family Issues")),]$gipc_bw <- 1
        
        # SPD: Labor, Employment, Immigration
        # SPD_bw: Labor, Employment, Immigration
        billxparty[which(billxparty$fraktion=="SPD" & 
                           billxparty$cap1_field=="Labor, Employment and Immigration"),]$gipc <- 1
        billxparty[which(billxparty$fraktion=="SPD" & 
                           billxparty$cap1_field=="Labor, Employment and Immigration"),]$gipc_bw <- 1
        
        # Gruene: Environment; Agriculture
        # Gruene_bw: Environment; Agriculture
        billxparty[which(billxparty$fraktion=="Gruene" & 
                           (billxparty$cap1_field=="Environment" |
                              billxparty$cap1_field=="Agriculture")),]$gipc <- 1
        billxparty[which(billxparty$fraktion=="Gruene" & 
                           (billxparty$cap1_field=="Environment" |
                              billxparty$cap1_field=="Agriculture")),]$gipc_bw <- 1
        
        # FDP: Macroeconomy; Banking, Finance and Domestic Commerce; Civil Rights, Minority Issues and Civil Liberties
        # FDP_bw: Macroeconomy; Banking, Finance and Domestic Commerce; Civil Rights, Minority Issues and Civil Liberties
        billxparty[which(billxparty$fraktion=="FDP/DVP" & 
                           (billxparty$cap1_field=="Macroeconomy" |
                              billxparty$cap1_field=="Banking, Finance and Domestic Commerce" |
                              billxparty$cap1_field=="Civil Rights, Minority Issues and Civil Liberties")),]$gipc <- 1
        billxparty[which(billxparty$fraktion=="FDP/DVP" & 
                           (billxparty$cap1_field=="Macroeconomy" |
                              billxparty$cap1_field=="Banking, Finance and Domestic Commerce" |
                              billxparty$cap1_field=="Civil Rights, Minority Issues and Civil Liberties")),]$gipc_bw <- 1
        
        # AfD: Law, Crime and Family Issues; Macroeconomy
        # AfD_bw: Law, Crime and Family Issues; Macroeconomy
        billxparty[which(billxparty$fraktion=="AfD" & 
                           (billxparty$cap1_field=="Law, Crime and Family Issues" |
                              billxparty$cap1_field=="Macroeconomy")),]$gipc <- 1
        billxparty[which(billxparty$fraktion=="AfD" & 
                           (billxparty$cap1_field=="Law, Crime and Family Issues" |
                              billxparty$cap1_field=="Macroeconomy")),]$gipc_bw <- 1
        
        amendmentprop$issue_own <- 0
        amendmentprop$issue_own[which((amendmentprop$cap1_field=="Environment" | amendmentprop$cap1_field=="Agriculture") & amendmentprop$GRUENE==1)] <- 1
        amendmentprop$issue_own[which((amendmentprop$cap1_field=="Law, Crime and Family Issues" | amendmentprop$cap1_field=="Banking, Finance and Domestic Commerce" | amendmentprop$cap1_field=="Macroeconomy") & amendmentprop$CDU==1)] <- 1
        amendmentprop$issue_own[which((amendmentprop$cap1_field=="Law, Crime and Family Issues" | amendmentprop$cap1_field=="Macroeconomy") & amendmentprop$AfD==1)] <- 1
        amendmentprop$issue_own[which((amendmentprop$cap1_field=="Banking, Finance and Domestic Commerce" | amendmentprop$cap1_field=="Macroeconomy" | amendmentprop$cap1_field=="Civil Rights, Minority Issues and Civil Liberties") & amendmentprop$`FDP/DVP`==1)] <- 1
        amendmentprop$issue_own[which((amendmentprop$cap1_field=="Labor, Employment and Immigration" | amendmentprop$cap1_field=="Social Welfare") & amendmentprop$SPD==1)] <- 1
        
      
        ### recategorize all policy fields with less than 10 observations into "Other"
        billxparty$cap1_field[billxparty$cap1_field=="Defense" |
                                billxparty$cap1_field=="International Affairs and Foreign Aid" |
                                billxparty$cap1_field=="State and Local Government Administration" |
                                billxparty$cap1_field=="Public Lands and Water Management"] <- "Other"
        
      
#' ------------------------------------------------------------------
# 2. construct dependent variable weighted Levenshtein distance -----
#' ------------------------------------------------------------------

    #' -----------------------------------------------------------------------------
    # 2.1 construct party identifiers: amendments of multiple parties included -----
    #' -----------------------------------------------------------------------------
    
      cdu_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                        amendmentprop$CDU == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
      cdu_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                          amendmentprop$CDU == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
      cdu_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                          amendmentprop$CDU == 1 & amendmentprop$SPD == 0 & amendmentprop$`FDP/DVP` == 0 & amendmentprop$AfD == 0)
      cdu_amends <- c(cdu_wp14, cdu_wp15, cdu_wp16)
      
      
      spd_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                          amendmentprop$SPD == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
      spd_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                          amendmentprop$SPD == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
      spd_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                          amendmentprop$SPD == 1 & amendmentprop$CDU == 0 & amendmentprop$GRUENE == 0)
      spd_amends <- c(spd_wp14, spd_wp15, spd_wp16)
      
      
      gruene_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                          amendmentprop$GRUENE == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
      gruene_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                          amendmentprop$GRUENE == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
      gruene_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                          amendmentprop$GRUENE == 1 & amendmentprop$SPD == 0 & amendmentprop$`FDP/DVP` == 0 & amendmentprop$AfD == 0)
      gruene_amends <- c(gruene_wp14, gruene_wp15, gruene_wp16)
      
      
      fdp_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                             amendmentprop$`FDP/DVP` == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
      fdp_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                             amendmentprop$`FDP/DVP` == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
      fdp_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                             amendmentprop$`FDP/DVP` == 1 & amendmentprop$CDU == 0 & amendmentprop$GRUENE == 0)
      fdp_amends <- c(fdp_wp14, fdp_wp15, fdp_wp16)
      
      
      afd_amends <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                          amendmentprop$AfD == 1 & amendmentprop$CDU == 0 & amendmentprop$GRUENE == 0)
      
  
    #' ---------------------------------------------------
    # 2.2 weighted Levenshtein distance (word level) -----
    #' ---------------------------------------------------
     
      billxparty$worddist_weighted <- 0
    
      # data frames with relevant amendments
      amendmentprop_cdu <- amendmentprop[cdu_amends,]
      amendmentprop_spd <- amendmentprop[spd_amends,]
      amendmentprop_gruene <- amendmentprop[gruene_amends,]
      amendmentprop_fdp <- amendmentprop[fdp_amends,]
      amendmentprop_afd <- amendmentprop[afd_amends,]   
      amendmentprop_valid <- rbind(amendmentprop_cdu, amendmentprop_spd, 
                                   amendmentprop_gruene, amendmentprop_fdp, 
                                   amendmentprop_afd)
      rownames(amendmentprop_valid) <- 1:nrow(amendmentprop_valid)
      
      # construct word distances
      for (i in 1:nrow(billxparty)) {
        
        if (billxparty$fraktion[i] == "CDU") 
          amend_ids <- which(amendmentprop_valid$idDrucksacheLegis == billxparty$idDrucksache[i] & amendmentprop_valid$CDU == 1)
        if (billxparty$fraktion[i] == "SPD") 
          amend_ids <- which(amendmentprop_valid$idDrucksacheLegis == billxparty$idDrucksache[i] & amendmentprop_valid$SPD == 1)
        if (billxparty$fraktion[i] == "Gruene") 
          amend_ids <- which(amendmentprop_valid$idDrucksacheLegis == billxparty$idDrucksache[i] & amendmentprop_valid$GRUENE == 1)
        if (billxparty$fraktion[i] == "FDP/DVP") 
          amend_ids <- which(amendmentprop_valid$idDrucksacheLegis == billxparty$idDrucksache[i] & amendmentprop_valid$`FDP/DVP` == 1)
        if (billxparty$fraktion[i] == "AfD") 
          amend_ids <- which(amendmentprop_valid$idDrucksacheLegis == billxparty$idDrucksache[i] & amendmentprop_valid$AfD== 1)
       
        if (length(amend_ids) > 0) 
         billxparty$worddist_weighted[i] <- sum(amendmentprop_valid$worddist_weighted[amend_ids])
         
      }
      
    
      
      
#' ----------------------------------------
# 3. construct independent variables ------
#' ----------------------------------------
  
    #' -----------------
    # size of faction
    #' -----------------
    
      billxparty$factionsize <- NA
      billxparty$parlsize <- NA
      billxparty$rel_factionsize <- NA
      
      # WP 14
      billxparty$parlsize[substr(billxparty$idDrucksache, 1, 2) == "14"] <- 139
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "14" & billxparty$fraktion == "CDU"] <- 69
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "14" & billxparty$fraktion == "SPD"]  <- 38
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "14" & billxparty$fraktion == "Gruene"] <- 17
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "14" & billxparty$fraktion == "FDP/DVP"] <- 15
      
      # WP 15
      billxparty$parlsize[substr(billxparty$idDrucksache, 1, 2) == "15"] <- 138 
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "15" & billxparty$fraktion == "CDU"] <- 60
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "15" & billxparty$fraktion == "SPD"]  <- 35
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "15" & billxparty$fraktion == "Gruene"] <- 36
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "15" & billxparty$fraktion == "FDP/DVP"] <- 7
      
      # WP 16
      billxparty$parlsize[substr(billxparty$idDrucksache, 1, 2) == "16"] <- 143
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "16" & billxparty$fraktion == "CDU"] <- 42
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "16" & billxparty$fraktion == "SPD"]  <- 19
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "16" & billxparty$fraktion == "Gruene"] <- 47 
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "16" & billxparty$fraktion == "FDP/DVP"] <- 12
      billxparty$factionsize[substr(billxparty$idDrucksache, 1, 2) == "16" & billxparty$fraktion == "AfD"] <- 23
      
      billxparty$rel_factionsize <- billxparty$factionsize/billxparty$parlsize
    
    
    #' ---------------------------
    # time until next election  
    #' ---------------------------
      
      billxparty$datedist <- NA
      
      billxparty$datestemp <- gsub("[.]", "/", billxparty$datestemp)
      billxparty$datestemp <- gsub(" ", "", billxparty$datestemp)
      
      billxparty$datedist[substr(billxparty$idDrucksache, 1, 2) == "14"] <- as.Date("2011-03-27") - as.Date(as.character(billxparty$datestemp[substr(billxparty$idDrucksache, 1, 2) == "14"]), format="%d/%m/%Y")
      billxparty$datedist[substr(billxparty$idDrucksache, 1, 2) == "15"] <- as.Date("2016-03-13") - as.Date(as.character(billxparty$datestemp[substr(billxparty$idDrucksache, 1, 2) == "15"]), format="%d/%m/%Y")
      billxparty$datedist[substr(billxparty$idDrucksache, 1, 2) == "16"] <- as.Date("2021-03-14") - as.Date(as.character(billxparty$datestemp[substr(billxparty$idDrucksache, 1, 2) == "16"]), format="%d/%m/%Y") 
      
      # measure in months until next election    
      billxparty$datedist <- billxparty$datedist/30
    
    
    #' -------------------------------
    # opposition chair in committee
    #' -------------------------------
      
      # if laws have not been sent to a committee, code as 0 as well
      billxparty$oppchair_committee[which(is.na(billxparty$oppchair_committee))] <- 0
    
    #' -------------------------------------------------
    # bill importance
    # relative position in protocol of first reading
    #' -------------------------------------------------
    
      billxparty$prot_relpos_1[which(is.na(billxparty$prot_relpos_1))] <- 7/18
      billxparty$bill_imp <- (billxparty$prot_relpos_1)^(-1)
      range01 <- function(x){(x-min(x))/(max(x)-min(x))}
      billxparty$bill_imp <- range01(billxparty$bill_imp) * 100
    
    #' -------------------------------
    # bill length
    #' -------------------------------
    
      billxparty$bill_length <- NA
    
      for (i in 1: nrow(billxparty)) {
        
        billxparty$bill_length[i] <- 
          length(which(str_detect(billxparty$rawtext_left_orig[i][[1]],  
                           paste(str_c("^Artikel ", 1:300,"$"), collapse = "|"))))
      }
    
    # if there are no articles, perceive law as one article
    billxparty$bill_length[which(billxparty$bill_length == 0)] <- 1
    
    
    ### construct binary amendment variable
    billxparty$amend <- 0
    
    cdu_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                        amendmentprop$CDU == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
    cdu_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                        amendmentprop$CDU == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
    cdu_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                        amendmentprop$CDU == 1 & amendmentprop$SPD == 0 & amendmentprop$`FDP/DVP` == 0 & amendmentprop$AfD == 0)
    cdu_amends <- c(cdu_wp14, cdu_wp15, cdu_wp16)
    
    
    spd_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                        amendmentprop$SPD == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
    spd_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                        amendmentprop$SPD == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
    spd_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                        amendmentprop$SPD == 1 & amendmentprop$CDU == 0 & amendmentprop$GRUENE == 0)
    spd_amends <- c(spd_wp14, spd_wp15, spd_wp16)
    
    
    gruene_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                           amendmentprop$GRUENE == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
    gruene_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                           amendmentprop$GRUENE == 1 & amendmentprop$CDU == 0 & amendmentprop$`FDP/DVP` == 0)
    gruene_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                           amendmentprop$GRUENE == 1 & amendmentprop$SPD == 0 & amendmentprop$`FDP/DVP` == 0 & amendmentprop$AfD == 0)
    gruene_amends <- c(gruene_wp14, gruene_wp15, gruene_wp16)
    
    
    fdp_wp14 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "14" &
                        amendmentprop$`FDP/DVP` == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
    fdp_wp15 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "15" &
                        amendmentprop$`FDP/DVP` == 1 & amendmentprop$SPD == 0 & amendmentprop$GRUENE == 0)
    fdp_wp16 <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                        amendmentprop$`FDP/DVP` == 1 & amendmentprop$CDU == 0 & amendmentprop$GRUENE == 0)
    fdp_amends <- c(fdp_wp14, fdp_wp15, fdp_wp16)
    
    
    afd_amends <- which(substr(amendmentprop$idDrucksache, 1, 2) == "16" &
                          amendmentprop$AfD == 1 & amendmentprop$CDU == 0 & amendmentprop$GRUENE == 0)
    
    billxparty$amend[which(is.element(billxparty$idDrucksache, amendmentprop[cdu_amends,]$idDrucksacheLegis) & billxparty$fraktion=="CDU")] <- 1
    billxparty$amend[which(is.element(billxparty$idDrucksache, amendmentprop[spd_amends,]$idDrucksacheLegis) & billxparty$fraktion=="SPD")] <- 1
    billxparty$amend[which(is.element(billxparty$idDrucksache, amendmentprop[gruene_amends,]$idDrucksacheLegis) & billxparty$fraktion=="Gruene")] <- 1
    billxparty$amend[which(is.element(billxparty$idDrucksache, amendmentprop[fdp_amends,]$idDrucksacheLegis) & billxparty$fraktion=="FDP/DVP")] <- 1
    billxparty$amend[which(is.element(billxparty$idDrucksache, amendmentprop[afd_amends,]$idDrucksacheLegis) & billxparty$fraktion=="AfD")] <- 1
    
    
    #' -------------------------------------------
    # origin
    # for each bill x party observation:
    # origin of amendment?
    # no amendment: none
    # opposition amendment: opp
    # government amendment: gov
    #' -------------------------------------------
    
      billxparty$origin <- NA
      
      # government amendment
      gov_14 <- which(substr(billxparty$idDrucksache, 1, 2) == "14" &
                     (billxparty$fraktion=="CDU" | billxparty$fraktion=="FDP/DVP"))
      gov_15 <- which(substr(billxparty$idDrucksache, 1, 2) == "15" &
                     (billxparty$fraktion=="SPD" | billxparty$fraktion=="Gruene"))
      gov_16 <- which(substr(billxparty$idDrucksache, 1, 2) == "16" &
                     (billxparty$fraktion=="CDU" | billxparty$fraktion=="Gruene"))
      billxparty$origin[c(gov_14, gov_15, gov_16)] <- "gov"
      
      # opposition amendment
      opp_14 <- which(substr(billxparty$idDrucksache, 1, 2) == "14" &
                     (billxparty$fraktion=="SPD" | billxparty$fraktion=="Gruene"))
      opp_15 <- which(substr(billxparty$idDrucksache, 1, 2) == "15" &
                     (billxparty$fraktion=="CDU" | billxparty$fraktion=="FDP/DVP"))
      opp_16 <- which(substr(billxparty$idDrucksache, 1, 2) == "16" &
                     (billxparty$fraktion=="SPD" | billxparty$fraktion=="FDP/DVP" | billxparty$fraktion=="AfD"))
      billxparty$origin[c(opp_14, opp_15, opp_16)] <- "opp"
      

    #' -------------------------------------
    # add variable on policy distance
    #' -------------------------------------
    
      posits <- read.dta13("pwib2020_final.dta")
      posits <- posits %>% 
        filter(land_name == "Baden-Württemberg", 
               year > 2005) 
      posits$party <- recode(posits$party, cdu = "CDU", 
                             fdp = "FDP/DVP", gru = "Gruene",
                             spd = "SPD", afd = "AfD", pds = "Linke")
      
      id_14 <- which(billxparty$Landesregierung == 1 & substr(billxparty$idDrucksache, 1, 2) == "14")
      billxparty[id_14, c("CDU", "FDP/DVP")] <- 1
      id_15 <- which(billxparty$Landesregierung == 1 & substr(billxparty$idDrucksache, 1, 2) == "15")
      billxparty[id_15, c("GRUENE", "SPD")] <- 1
      id_16 <- which(billxparty$Landesregierung == 1 & substr(billxparty$idDrucksache, 1, 2) == "16")
      billxparty[id_16, c("GRUENE", "CDU")] <- 1
      
      billxparty$year <- NA
      billxparty$year[which(substr(billxparty$idDrucksache, 1, 2) == "14")] <- 2006
      billxparty$year[which(substr(billxparty$idDrucksache, 1, 2) == "15")] <- 2011
      billxparty$year[which(substr(billxparty$idDrucksache, 1, 2) == "16")] <- 2016
      
      billxparty$econ_dist <- billxparty$lire_dist <- billxparty$soci_dist <- billxparty$mean_dist <- NA
      
      for (row in 1: nrow(billxparty)) {
        
        # economic distance
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "FDP/DVP"] == 1)
          billxparty[row, "econ_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "econ_pos"], posits[which(posits$year == billxparty[row, "year"] & posits$party == "FDP/DVP"), "econ_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "econ_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "FDP/DVP"] == 1 & billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "econ_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "econ_pos"], 
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "FDP/DVP"), "econ_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "econ_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "econ_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "econ_pos"])
        
        if (billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "econ_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "econ_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "econ_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "econ_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "econ_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "econ_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "econ_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "econ_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "econ_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "econ_pos"], 
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "econ_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "econ_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "econ_pos"])
        
        # left/right distance
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "FDP/DVP"] == 1)
          billxparty[row, "lire_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "lire_pos"], posits[which(posits$year == billxparty[row, "year"] & posits$party == "FDP/DVP"), "lire_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "lire_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "FDP/DVP"] == 1 & billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "lire_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "lire_pos"], 
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "FDP/DVP"), "lire_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "lire_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "lire_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "lire_pos"])
        
        if (billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "lire_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "lire_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "lire_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "lire_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "lire_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "lire_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "lire_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "lire_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "lire_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "lire_pos"], 
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "lire_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "lire_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "lire_pos"])
        
        
        # social distance
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "FDP/DVP"] == 1)
          billxparty[row, "soci_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "soci_pos"], posits[which(posits$year == billxparty[row, "year"] & posits$party == "FDP/DVP"), "soci_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "soci_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "FDP/DVP"] == 1 & billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "soci_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "soci_pos"], 
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "FDP/DVP"), "soci_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "soci_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "soci_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "soci_pos"])
        
        if (billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "soci_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "soci_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "soci_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "soci_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "soci_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "soci_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "soci_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "soci_pos"])
        
        if (billxparty[row, "CDU"] == 1 & billxparty[row, "SPD"] == 1 & billxparty[row, "GRUENE"] == 1)
          billxparty[row, "soci_dist"] <- 
            abs(mean(c(posits[which(posits$year == billxparty[row, "year"] & posits$party == "CDU"), "soci_pos"], 
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "SPD"), "soci_pos"],
                       posits[which(posits$year == billxparty[row, "year"] & posits$party == "Gruene"), "soci_pos"])) - posits[which(posits$year == billxparty[row, "year"] & posits$party == billxparty$fraktion[row]), "soci_pos"])
        
        # mean distance
        billxparty$mean_dist[row] <- mean(c(billxparty$econ_dist[row], billxparty$lire_dist[row], billxparty$soci_dist[row]))
        
      }
      

#' ----------------------------------
# 4. construct reduced dataset ------
#' ----------------------------------
  
  
  #' -------------------------------------------------------------------------
  # 4.1 amendmentprop - only with amendments that inherit gov/opp divide -----
  #' -------------------------------------------------------------------------
    
    amendmentprop_govopp <- amendmentprop
    
    # delete amendments authored across gov/opp parties
    across <- which(rowSums(cbind(amendmentprop$SPD, amendmentprop$CDU, 
                                  amendmentprop$GRUENE, amendmentprop$`FDP/DVP`,
                                  amendmentprop$AfD)) > 2)  
    amendmentprop_govopp <- amendmentprop[-across,]
    
    
  #' ------------------- 
  # 4.2 billxparty -----
  #' --------------------
  
    opp14 <- which(substr(billxparty$idDrucksache, 1, 2) == "14" & 
                   (billxparty$fraktion == "SPD" | billxparty$fraktion == "Gruene"))
    opp15 <- which(substr(billxparty$idDrucksache, 1, 2) == "15" & 
                     (billxparty$fraktion == "CDU" | billxparty$fraktion == "FDP/DVP"))
    opp16 <- which(substr(billxparty$idDrucksache, 1, 2) == "16" & 
                     (billxparty$fraktion == "SPD" | billxparty$fraktion == "FDP/DVP" | billxparty$fraktion == "AfD"))
    billxparty_opp <- billxparty[c(opp14, opp15, opp16),]
    
  
    # recategorize all policy fields with less than 10 observations into "Other"
    billxparty_opp$cap1_field[billxparty_opp$cap1_field=="Defense" |
                                billxparty_opp$cap1_field=="International Affairs and Foreign Aid" |
                                billxparty_opp$cap1_field=="State and Local Government Administration" |
                                billxparty_opp$cap1_field=="Public Lands and Water Management"] <- "Other"
    
    # only subset bill x party observations which include amendments
    billxparty_reduced <- billxparty[which(billxparty$amend==1),]
  
    
  #' ------------------------------------------------------------------- 
  # 4.3 construct information for Table 3 - descriptive statistics -----
  #' -------------------------------------------------------------------
    
    log_print("Information for Table 3: Descriptive statistics of key variables.")
    
    # part A - dependent variable 
    log_print("Part A: Dependent variable")
    log_print(summary(billxparty_reduced$worddist_weighted[which(billxparty_reduced$origin=="opp")])) # opposition
    log_print(summary(billxparty_reduced$worddist_weighted[which(billxparty_reduced$origin=="gov")])) # government
    log_print(summary(billxparty_reduced$worddist_weighted)) # overall
    
    # part B - independent variables 
    log_print("Part B: Independent variables")
    log_print(table(billxparty_reduced$origin)) # opposition, mean = 152/(152+125) = 0.55
    log_print(summary(billxparty_reduced$bill_imp)) # bill importance
    log_print(sd(billxparty_reduced$bill_imp)) # bill importance
    log_print(summary(billxparty_reduced$factionsize)) # party group size
    log_print(sd(billxparty_reduced$factionsize)) # party group size
    log_print(summary(billxparty_reduced$datedist)) # date distance
    log_print(sd(billxparty_reduced$datedist)) # date distance
    log_print(summary(billxparty_reduced$lire_dist)) # ideological distance
    log_print(sd(billxparty_reduced$lire_dist)) # ideological distance
    log_print(table(billxparty_reduced$oppchair_committee)) # opposition chair committee, mean = 147/(147+130) = 0.53
    log_print(summary(billxparty_reduced$bill_length)) # bill length
    log_print(sd(billxparty_reduced$bill_length)) # bill length
    
    
#' ----------------------------------
# 5. graphical visualizations -------
#' ----------------------------------
 
  #' ------------------------------------------------
  # different transformations for bill importance
  #' ------------------------------------------------
  
    # scale each transformed variable between 0 and 1
    range01 <- function(x){(x-min(x))/(max(x)-min(x))}
    
    tikz('transf.tex', standAlone = TRUE, width=7, height=7)
    
      par(mar=c(5.1,4.5,4.1,2.1))
      
      # c=1
      imp <- (billxparty_reduced$prot_relpos_1)^(-1)
      imp <- range01(imp) * 100
      plot(billxparty_reduced$prot_relpos_1, imp, lwd=3, bty="n", xlim=c(0,1), ylim=c(0,100),
           xlab="Relative Position of Bill in Plenary Protocol", ylab="Bill Importance Measure (Rescaled)", 
           main="", cex.lab=1.8, cex.axis=1.8, las=1)
      lines(billxparty_reduced$prot_relpos_1[order(billxparty_reduced$prot_relpos_1)], 
            imp[order(billxparty_reduced$prot_relpos_1)], lwd=3)
      
      # c=0.75
      imp <- (billxparty_reduced$prot_relpos_1)^(-0.75)
      imp <- range01(imp) * 100
      lines(billxparty_reduced$prot_relpos_1, imp, type="p", col="white")
      lines(billxparty_reduced$prot_relpos_1[order(billxparty_reduced$prot_relpos_1)], 
            imp[order(billxparty_reduced$prot_relpos_1)], lwd=6, col="darkgrey")
    
      for (i in seq(1.25, 2.75, 0.25)) {
        imp <- (billxparty_reduced$prot_relpos_1)^(-i)
        imp <- range01(imp) * 100
        lines(billxparty_reduced$prot_relpos_1, imp, type="p", col="white")
        lines(billxparty_reduced$prot_relpos_1[order(billxparty_reduced$prot_relpos_1)], 
              imp[order(billxparty_reduced$prot_relpos_1)], lty=2)
      }
    
      # c=4
      imp <- (billxparty_reduced$prot_relpos_1)^(-3)
      imp <- range01(imp) * 100
      lines(billxparty_reduced$prot_relpos_1, imp, type="p", col="white")
      lines(billxparty_reduced$prot_relpos_1[order(billxparty_reduced$prot_relpos_1)], 
            imp[order(billxparty_reduced$prot_relpos_1)], lwd=6, col="lightgrey")
      
        
      legend(0.55, 110, legend=c("c=0.75", "c=1", "c=3", "alternative power\ntransformations"), 
             lty=c(1,1,1,2), lwd=c(3,3,3,1), col=c("darkgrey", "black", "lightgrey", "black"), 
             pch=c(NA, 1, NA, NA), bty="n", cex=1.8)
    
    dev.off()
    tools::texi2dvi('transf.tex',pdf=T)
    system(paste(getOption('pdfviewer'),'transf.pdf'))
    
  
    #' ------------------------------
    # 5.1 policy fields
    #' ------------------------------
    
      amendmentprop$amend_cat <- NA
      
      destructive_words <- c("streichen", "gestrichen", 
                             "aufheben", "aufgehoben", "aufzuheben", 
                             "löschen", "gelöscht", 
                             "entfallen", "entfällt")
      
      constructive_words <- c("anfügen", "angefügt", "anzufügen", 
                              "hinzufügen", "hinzugefügt", "hinzuzufügen", 
                              "ergänzen", "ergänzt", 
                              "voranstellen", "vorangestellt", "voranzustellen",
                              "einfügen", "eingefügt", "einzufügen", 
                              "einschieben", "eingeschoben", "einzuschieben", 
                              "ersetzen", "ersetzt", 
                              "fassen", "gefasst", 
                              "neufassen", "neugefasst", "neuzufassen", 
                              "aufnehmen", "aufgenommen", "aufzunehmen",
                              "Fassung", "Fasssung")
      
      
      for (i in 1: nrow(amendmentprop)) {
        
        # collapse list
        string <- unlist(amendmentprop$rawtext_left[i])
        string <- paste(string, collapse = " ")
        
        # remove all quotations
        string <- rm_between(string, "„", "“")
        
        # detect signaling words
        if(length(which(str_detect(string, constructive_words))) > 0)
          amendmentprop$amend_cat[i] <- "constructive"
        
        if(length(which(str_detect(string, constructive_words))) == 0 & 
           length(which(str_detect(string, destructive_words))) > 0)
          amendmentprop$amend_cat[i] <- "destructive"
        
        # constructive if there is no action word at all but "geändert"
        if(length(which(str_detect(string, constructive_words))) == 0 & 
           length(which(str_detect(string, destructive_words))) == 0 &
           length(which(str_detect(string, "geändert"))) > 0)
          amendmentprop$amend_cat[i] <- "constructive"
        
      }
      
      amendmentprop$amend_cat2 <- NA
      amendmentprop$amend_cat2[which(amendmentprop$worddist_weighted > 0)] <- "constructive"
      amendmentprop$amend_cat2[which(amendmentprop$worddist_weighted <= 0)] <- "destructive"
      
    
      # table(amendmentprop$cap1_field)
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Defense" |
                               amendmentprop$cap1_field=="Energy" |
                               amendmentprop$cap1_field=="Macroeconomy" | 
                               amendmentprop$cap1_field=="Transportation")] <- "Other"
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Banking, Finance and Domestic Commerce")] <- "Banking and Finance"
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Community Developlemnt and Housing Issues")] <- "Community Development"
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Civil Rights, Minority Issues and Civil Liberties")] <- "Civil Rights and Liberties"
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Space, Science, Technology and Communications")] <- "Science and Technology"
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Public Lands and Water Management")] <- "Lands and Water Management"
      amendmentprop$cap1_field[which(amendmentprop$cap1_field=="Labor, Employment and Immigration")] <- "Labor and Immigration"
      
      cap1_bars <- as.data.frame(rbind(c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[1],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[1],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[1]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[1],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[1],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[1]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[2],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[2],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[2]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[2],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[2],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[2]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[3],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[3],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[3]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[3],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[3],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[3]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[4],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[4],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[4]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[4],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[4],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[4]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[5],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[5],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[5]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[5],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[5],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[5]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[6],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[6],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[6]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[6],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[6],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[6]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[7],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[7],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[7]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[7],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[7],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[7]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[8],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[8],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[8]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[8],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[8],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[8]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[9],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[9],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[9]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[9],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[9],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[9]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[10],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[10],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[10]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[10],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[10],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[10]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[11],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[11],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[11]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[11],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[11],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[11]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[12],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[12],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[12]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[12],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[12],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[12]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[13],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[13],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[13]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[13],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[13],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[13]),
                                       c("constructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[14],]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[14],]$amend_cat2=="constructive")), unique(amendmentprop$cap1_field)[14]),
                                       c("destructive", length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[14],]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$cap1_field==unique(amendmentprop$cap1_field)[14],]$amend_cat2=="destructive")), unique(amendmentprop$cap1_field)[14])
                                  ))
      colnames(cap1_bars) <- c("Category", "amend_cat", "amend_cat2", "cap1")                    
      cap1_bars$amend_cat <- as.numeric(as.character(cap1_bars$amend_cat))
      cap1_bars$amend_cat2 <- as.numeric(as.character(cap1_bars$amend_cat2))
    
      cap1_bars$cap1 <- factor(cap1_bars$cap1, levels =  c("Agriculture",
                                                           "Banking and Finance",
                                                           "Other",
                                                           "Lands and Water Management",
                                                           "Labor and Immigration",
                                                           "Community Development",
                                                           "Government Operations",
                                                           "Law, Crime and Family Issues",
                                                           "Healthcare",
                                                           "Education",
                                                           "Social Welfare",
                                                           "Environment",
                                                           "Civil Rights and Liberties",
                                                           "Science and Technology"
      ))
      
      cap1_bars$amend_cat_perc <- NA
      cap1_bars$amend_cat2_perc <- NA
      for (row in 1:nrow(cap1_bars)) {
        ids <- which(cap1_bars$cap1 == cap1_bars$cap1[row])
        cap1_bars$amend_cat_perc[row] <- cap1_bars$amend_cat[row] / sum(cap1_bars$amend_cat[ids])
        cap1_bars$amend_cat2_perc[row] <- cap1_bars$amend_cat2[row] / sum(cap1_bars$amend_cat2[ids])
      }
     
      
      
      
      cap1_bars <- cap1_bars[cap1_bars$cap1!="Other",]
      
      cap1_plot <- ggplot(data=cap1_bars, aes(x=cap1, y=amend_cat2_perc, fill=Category)) +
        geom_bar(stat="identity", color="black", position="stack") +
        xlab("") + ylab("Percentage") +
        scale_y_continuous(breaks=seq(0, 1, 0.25), limits=c(0,1.08)) 
       
      library(grid) # for adding text
      
      tikz('policy_fields.tex', standAlone = TRUE, width=15, height=11)
      ypos <- 1.06
      cexsize <- 6.5
      
      cap1_plot +
        theme_bw(base_size=27) + scale_fill_grey() + 
        theme(axis.text.x = element_text(angle=80, hjust = 0.97, vjust=0.98)) +
        annotate("text", x="Education", y=ypos, label= "(161)", cex=cexsize) +
        annotate("text", x="Banking and Finance", y=ypos, label= "(10)", cex=cexsize) +
        annotate("text", x="Government Operations", y=ypos, label= "(73)", cex=cexsize) +
        annotate("text", x="Social Welfare", y=ypos, label= "(11)", cex=cexsize) +
        annotate("text", x="Healthcare", y=ypos, label= "(34)", cex=cexsize) +
        annotate("text", x="Agriculture", y=ypos, label= "(11)", cex=cexsize) +
        annotate("text", x="Civil Rights and Liberties", y=ypos, label= "(29)", cex=cexsize) +
        annotate("text", x="Environment", y=ypos, label= "(22)", cex=cexsize) +
        annotate("text", x="Law, Crime and Family Issues", y=ypos, label= "(58)", cex=cexsize) +
        annotate("text", x="Community Development", y=ypos, label= "(27)", cex=cexsize) +
        annotate("text", x="Lands and Water Management", y=ypos, label= "(16)", cex=cexsize) +
        annotate("text", x="Science and Technology", y=ypos, label= "(40)", cex=cexsize) +
        annotate("text", x="Labor and Immigration", y=ypos, label= "(18)", cex=cexsize)
       
      dev.off()
      tools::texi2dvi('policy_fields.tex',pdf=T)
      system(paste(getOption('pdfviewer'),'policy_fields.pdf'))
      
   
    #' ----------------------------------------------------
    # 5.3 party groups x policy fields
    # do parties work differently in their prime issue?
    #' ----------------------------------------------------
    
      # with data amendmentprop
      prime_issue_bars <- as.data.frame(rbind(c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat2=="constructive")), "Overall"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat2=="destructive")), "Overall"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat2=="constructive")), "CDU"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat2=="destructive")), "CDU"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat2=="constructive")), "SPD"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat2=="destructive")), "SPD"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat2=="constructive")), "Greens"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat2=="destructive")), "Greens"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="constructive")), "FDP"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="destructive")), "FDP"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat2=="constructive")), "Overall"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat2=="destructive")), "Overall"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat2=="constructive")), "CDU"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat2=="destructive")), "CDU"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat2=="constructive")), "SPD"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat2=="destructive")), "SPD"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat2=="constructive")), "Greens"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat2=="destructive")), "Greens"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="constructive")), "FDP"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="destructive")), "FDP")
                                              ))
      
      prime_issue_bars <- as.data.frame(rbind(c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat2=="constructive")), "Overall"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1,]$amend_cat2=="destructive")), "Overall"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat2=="constructive")), "CDU"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$CDU==1,]$amend_cat2=="destructive")), "CDU"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat2=="constructive")), "SPD"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$SPD==1,]$amend_cat2=="destructive")), "SPD"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat2=="constructive")), "Greens"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$GRUENE==1,]$amend_cat2=="destructive")), "Greens"),
                                              c("constructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="constructive")), "FDP"),
                                              c("destructive", "Yes", length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==1 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="destructive")), "FDP"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat2=="constructive")), "Overall"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0,]$amend_cat2=="destructive")), "Overall"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat2=="constructive")), "CDU"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$CDU==1,]$amend_cat2=="destructive")), "CDU"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat2=="constructive")), "SPD"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$SPD==1,]$amend_cat2=="destructive")), "SPD"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat2=="constructive")), "Greens"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$GRUENE==1,]$amend_cat2=="destructive")), "Greens"),
                                              c("constructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="constructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="constructive")), "FDP"),
                                              c("destructive", "No", length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat=="destructive")), length(which(amendmentprop[amendmentprop$issue_own==0 & amendmentprop$`FDP/DVP`==1,]$amend_cat2=="destructive")), "FDP")
      ))
      
      colnames(prime_issue_bars) <- c("Category", "Issue_Ownership", "amend_cat", "amend_cat2", "party")     
      
      prime_issue_bars$party <- factor(prime_issue_bars$party, levels =  c("Overall", "CDU", "Greens", "FDP", "SPD", "AfD"))
      prime_issue_bars$amend_cat <- as.numeric(as.character(prime_issue_bars$amend_cat))
      prime_issue_bars$amend_cat2 <- as.numeric(as.character(prime_issue_bars$amend_cat2))
      
      prime_issue_bars$amend_cat_perc <- NA
      prime_issue_bars$amend_cat2_perc <- NA
      for (row in 1:nrow(prime_issue_bars)) {
        ids <- which(prime_issue_bars$party == prime_issue_bars$party[row] & prime_issue_bars$Issue_Ownership == prime_issue_bars$Issue_Ownership[row])
        prime_issue_bars$amend_cat2_perc[row] <- prime_issue_bars$amend_cat2[row] / sum(prime_issue_bars$amend_cat2[ids])
      }
      
      
      issueXparty <- ggplot(data=prime_issue_bars, aes(x=Issue_Ownership, y=amend_cat2_perc, fill=Category)) +
                      geom_bar(stat="identity", color="black", position="stack") + facet_grid(~ party) 
      
      tikz('issue_own.tex', standAlone = TRUE, width=15, height=9)
      issueXparty <- issueXparty +  
          theme_bw(base_size=27) + scale_fill_grey() +
          theme(axis.text.x = element_text(angle=80, hjust = 0.97, vjust=0.98)) +
          xlab("Issue Ownership") + ylab("Percentage") + scale_y_continuous(breaks=seq(0, 1, 0.25), limits=c(0,1.08)) 
        
        
      dat_text <- data.frame(
        label = c("(461)", "(66)", "(118)", "(28)", "(172)", "(32)", "(128)", "(19)", "(166)", "(31)", rep("", 10)),
        party   = c("Overall", "Overall", "CDU", "CDU", "Greens", "Greens", "FDP", "FDP", "SPD", "SPD"), 
        Category = c(rep("constructive", 10), rep("destructive", 10)),
        Issue_Ownership = rep(c("Yes", "No"), 10),
        x = rep(c(1, 2), 10),
        y= rep(1.05, 20)
      )
      
      dat_text$party <- factor(dat_text$party, levels = unique(dat_text$party))
      
      issueXparty + geom_text(
        data    = dat_text,
        mapping = aes(x=x, y = y, label = label), 
        cex = 6.5
       )
        
      dev.off()
      tools::texi2dvi('issue_own.tex',pdf=T)
      system(paste(getOption('pdfviewer'),'issue_own.pdf'))
      
   
#' ----------------------------------------
# 6. statistical modeling -----------------
#' ----------------------------------------

    m0 <- lmer(worddist_weighted ~ log(datedist) + oppchair_committee + log(bill_length) + cap1_field + (1 | idDrucksache), data = billxparty_reduced)
    
    
    #' -------------------------------------
    # 6.1 government-opposition divide -----
    #' -------------------------------------
    
      m1 <- lmer(worddist_weighted ~ origin + log(factionsize) +
                    log(datedist) + lire_dist +  oppchair_committee + log(bill_length) + cap1_field +
                    (1 | idDrucksache), data = billxparty_reduced)
      log_print("Table 4: Model 1")  
      log_print(summary(m1))
      
      # get coefficients and variances
      coef <- coef(m1)
      coef <- colMeans(coef$idDrucksache)
      vhat <- vcov(m1)
      sigma <- sqrt(sum(residuals(m1)^2) / (nrow(billxparty_reduced) - length(coef)))  
      draws <- mvrnorm(10000, coef, vhat)
      
      # set covariate values, rest to means, field to 'Education', vary over bill importance
      mus <- matrix(NA, 
                    nrow=100000, 
                    ncol=2)
      colnames(mus) <- c("opp", "gov")
      
      opp_setting <- c(1, 1, mean(log(billxparty_reduced$factionsize)), 
                         mean(log(billxparty_reduced$datedist)),
                         mean(billxparty_reduced$lire_dist),
                         mean(billxparty_reduced$oppchair_committee), 
                         mean(log(billxparty_reduced$bill_length)), 
                         rep(0,3), 1, rep(0,11))
        
      gov_setting <- c(1, 0, mean(log(billxparty_reduced$factionsize)), 
                         mean(log(billxparty_reduced$datedist)),
                         mean(billxparty_reduced$lire_dist),
                         mean(billxparty_reduced$oppchair_committee), 
                         mean(log(billxparty_reduced$bill_length)), 
                         rep(0,3), 1, rep(0,11))
        
      mus[,1] <- draws %*% as.matrix(opp_setting)
      mus[,2] <- draws %*% as.matrix(gov_setting)
     
      shares <- as.data.frame(matrix(NA, nrow=58, ncol=5))
      colnames(shares) <- c("val", "group", "share", "lower", "upper")
      shares$val <- rep(seq(-700, 700, 50), 2)
      shares$group <- c(rep("opp", 29), rep("gov", 29))
      
      row <- 0
      for (val in seq(-700, 700, 50)) {
       row <- row + 1
       shares$share[row] <- length(which(mus[,"opp"]<val)) / nrow(mus)
      }
      for (val in seq(-700, 700, 50)) {
        row <- row + 1
        shares$share[row] <- length(which(mus[,"gov"]<val)) / nrow(mus)
      }
      
      # get the 0.025 and 0.975 quantile values of the expected share
      lower_opp <- lower_gov <- upper_opp <- upper_gov <- rep(NA, 15)
      sharevar_opp <- sharevar_gov <- rep(NA, 1000)
      
      sim_iter <- 0
      for (val in seq(-700, 700, 50)) {
        
        sim_iter <- sim_iter + 1
        
        for (i in 1:1000) {
        
          draws <- mvrnorm(1000, coef, vhat)
          
          mus[,1] <- draws %*% as.matrix(opp_setting)
          mus[,2] <- draws %*% as.matrix(gov_setting)
         
          sharevar_opp[i] <- length(which(mus[,"opp"]<val)) / nrow(mus)
          sharevar_gov[i] <- length(which(mus[,"gov"]<val)) / nrow(mus)
          
        }
        
        lower_opp[sim_iter] <- quantile(sort(sharevar_opp), 0.001)
        upper_opp[sim_iter] <- quantile(sort(sharevar_opp), 0.999)
        lower_gov[sim_iter] <- quantile(sort(sharevar_gov), 0.001)
        upper_gov[sim_iter] <- quantile(sort(sharevar_gov), 0.999)
        
        print(val)
        
      }
      
      shares$lower <- c(lower_opp, lower_gov)
      shares$upper <- c(upper_opp, upper_gov)
      
      tikz('cum_plot.tex', standAlone = TRUE, width=15, height=9)
        ggplot(shares, aes(x=val, y=share, group=group, color=group)) + 
          geom_vline(xintercept = 0, linetype="dashed", 
                     color = "black", size=1) +
          geom_point(size=4) +
          geom_errorbar(aes(ymin=lower, ymax=upper), width=1, size=1.5,
                       position=position_dodge(0.05)) +
          scale_colour_manual(values = c("darkgrey", "black"), name = "Parliamentary\nGroup", labels = c("Government", "Opposition")) +
          scale_fill_manual(values = c("darkgrey", "black"), name = "Parliamentary\nGroup", labels = c("Government", "Opposition")) +
          scale_x_continuous(breaks=seq(-700, 800, 100)) +
          theme_bw() +
          guides(fill = guide_legend(reverse = TRUE), color = guide_legend(reverse = TRUE)) + 
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
                axis.line = element_line(colour = "black"),  panel.border = element_blank(), 
                axis.text=element_text(size=25), axis.title=element_text(size=25),
                legend.text=element_text(size=25), legend.title=element_text(size=25)) +
          xlab("Expected Constructiveness") + ylab("Cumulative Share of Expected Constructiveness")
      dev.off()
      tools::texi2dvi('cum_plot.tex',pdf=T)
      system(paste(getOption('pdfviewer'),'cum_plot.pdf'))
      
      
    
    
    #' --------------------------
    # 6.2 bill importance -------
    #' --------------------------
    
      m2_0 <- lmer(worddist_weighted ~ origin + bill_imp + log(factionsize) +
                   log(datedist) + lire_dist + oppchair_committee + log(bill_length) + cap1_field +
                   (1 | idDrucksache), data = billxparty_reduced)
      
      log_print("Table 4: Model 2a")  
      log_print(summary(m2_0))
      
      
      m2 <- lmer(worddist_weighted ~ origin * bill_imp + log(factionsize) +
                    log(datedist) + lire_dist + oppchair_committee + log(bill_length) + cap1_field +
                    (1 | idDrucksache), data = billxparty_reduced)
      
      log_print("Table 4: Model 2b")  
      log_print(summary(m2))
      
      # stargazer(m1, m2_0, m2, type = "latex", star.cutoffs = c(.05, .01, .001))
      
      # check the effect of different power transformations
      # for (i in seq(0.75,5,0.05)) {
      #   billxparty_reduced$bill_imp <- (billxparty_reduced$prot_relpos_1)^(-i)
      #   billxparty_reduced$bill_imp <- range01(billxparty_reduced$bill_imp)
      #   m3 <- lmer(worddist_weighted ~ origin * bill_imp + log(factionsize) +
      #                 log(datedist) + oppchair_committee + log(bill_length) + cap1_field +
      #                 (1 | idDrucksache), data = billxparty_reduced)
      #   print(coef(summary(m3))[,"t value"][23])
      # }
      # 
      # billxparty_reduced$bill_imp <- (billxparty_reduced$prot_relpos_1)^(-1)
      # billxparty_reduced$bill_imp <- range01(billxparty_reduced$bill_imp) * 100
      
      # get coefficients and variances
      coef <- coef(m2)
      coef <- colMeans(coef$idDrucksache)
      vhat <- vcov(m2)
      sigma <- sqrt(sum(residuals(m2)^2) / (nrow(billxparty_reduced) - length(coef)))  
      draws <- mvrnorm(10000, coef, vhat)
      
      # set covariate values, rest to means, field to 'Education', vary over bill importance
      mu_opp <- mu_gov <-  matrix(NA, 
                           nrow=10000, 
                           ncol=length(unique(billxparty_reduced$bill_imp)))
    
      for (i in 1:length(unique(billxparty_reduced$bill_imp))) {
    
        opp_setting <- c(1, 1, 0, mean(log(billxparty_reduced$factionsize)), 
                         mean(log(billxparty_reduced$datedist)),
                         mean(billxparty_reduced$lire_dist),
                         mean(billxparty_reduced$oppchair_committee), 
                         mean(log(billxparty_reduced$bill_length)), 
                         rep(0,3), 1, rep(0,11), 
                         sort(unique(billxparty_reduced$bill_imp))[i])
        
        gov_setting <- c(1, 0, sort(unique(billxparty_reduced$bill_imp))[i], 
                         mean(log(billxparty_reduced$factionsize)), 
                         mean(log(billxparty_reduced$datedist)),
                         mean(billxparty_reduced$lire_dist),
                         mean(billxparty_reduced$oppchair_committee), 
                         mean(log(billxparty_reduced$bill_length)), 
                         rep(0,3), 1, rep(0,11), 0)
        
        mu_opp[,i] <- draws %*% as.matrix(opp_setting)
        mu_gov[,i] <- draws %*% as.matrix(gov_setting)
      
      }
      
      # visualize expected values
      se_opp <- rep(NA, length(unique(billxparty_reduced$bill_imp)))
      se_gov <- rep(NA, length(unique(billxparty_reduced$bill_imp)))
      for (i in 1:ncol(mu_opp)) {
        se_opp[i] <- sd(mu_opp[,i])
        se_gov[i] <- sd(mu_gov[,i])
      }
      
      exp_values <- as.data.frame(cbind(c(colMeans(mu_opp), colMeans(mu_gov)),
                                        c(rep("Opposition", ncol(mu_opp)), rep("Government", ncol(mu_gov))),
                                        rep(sort(unique(billxparty_reduced$bill_imp)),2),
                                        c(se_opp, se_gov)
                                  )
                    )
      colnames(exp_values) <- c("exp", "group", "imp", "se")
      exp_values$exp <- as.numeric(as.character(exp_values$exp))
      exp_values$imp <- as.numeric(as.character(exp_values$imp))
      exp_values$se <- as.numeric(as.character(exp_values$se))
      
      tikz('bill_imp.tex', standAlone = TRUE, width=15, height=9)
        ggplot(exp_values, aes(x=imp, y=exp, color=factor(group))) +
          geom_smooth(method=lm, aes(fill=factor(group)), alpha=0.1) +
          geom_ribbon(aes(ymin=exp-se/2, 
                          ymax=exp+se/2), alpha=0.1) +
          geom_hline(yintercept=0, size=1.1, linetype="dashed", color = "black") +
          geom_rug(sides="b") + 
          scale_colour_manual(values = c("darkgrey", "black"), name = "", labels = c("", "")) +
          scale_fill_manual(values = c("darkgrey", "black"), name = "", labels = c("", "")) + 
          guides(color=F, fill=F, fill = guide_legend(reverse = TRUE), color = guide_legend(reverse = TRUE)) + 
          scale_x_continuous(breaks=seq(0, 45, 5), limits=c(0,45),) +
          theme_bw() +
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
                axis.line = element_line(colour = "black"),  panel.border = element_blank(), 
                axis.text=element_text(size=25), axis.title=element_text(size=25),
                legend.text=element_text(size=25), legend.title=element_text(size=25)) +
          xlab("Bill Importance") + ylab("Expected Constructiveness") +
          annotate(geom="text", x=35, y=850, label="Opposition", cex=8) + 
          annotate(geom="text", x=35, y=-550, label="Government", cex=8) 
      dev.off()
      tools::texi2dvi('bill_imp.tex',pdf=T)
      system(paste(getOption('pdfviewer'),'bill_imp.pdf'))
      
      
    #' --------------------------
    # 6.3 model for Greens ------
    #' --------------------------
    
      billxparty_gruene <- billxparty_reduced[which(billxparty_reduced$fraktion=="Gruene"),]
      billxparty_gruene$cap1_field <- as.factor(billxparty_gruene$cap1_field)
      billxparty_gruene$cap1_field <- relevel(billxparty_gruene$cap1_field, ref="Other")
      m3 <- lm(worddist_weighted ~ log(factionsize) +
                 log(datedist) + lire_dist + oppchair_committee + log(bill_length) + cap1_field, data = billxparty_gruene)
     
      # get coefficients and variances
      coef <- coef(m3)
      vhat <- vcov(m3)
      sigma <- sqrt(sum(residuals(m3)^2) / (nrow(billxparty_gruene) - length(coef)))  
      draws <- mvrnorm(10000, coef, vhat)
      
      # set covariate values, rest to means, field to 'Education', vary over bill importance
      mus <- matrix(NA, 
                    nrow=100000, 
                    ncol=2)
      colnames(mus) <- c("opp", "gov")
      
      opp_setting <- c(1, mean(log(billxparty_gruene$factionsize)), 
                       mean(log(billxparty_gruene$datedist)),
                       mean(billxparty_gruene$lire_dist),
                       mean(billxparty_gruene$oppchair_committee), 
                       mean(log(billxparty_gruene$bill_length)), 
                       rep(0,5), 1, rep(0,8))
      
      gov_setting <- c(1, mean(log(billxparty_gruene$factionsize)), 
                       mean(log(billxparty_gruene$datedist)),
                       mean(billxparty_gruene$lire_dist),
                       mean(billxparty_gruene$oppchair_committee), 
                       mean(log(billxparty_gruene$bill_length)), 
                       rep(0,7), 1, rep(0,6))
      
      mus[,1] <- draws %*% as.matrix(opp_setting)
      mus[,2] <- draws %*% as.matrix(gov_setting)
      
      shares <- as.data.frame(matrix(NA, nrow=40, ncol=5))
      colnames(shares) <- c("val", "group", "share", "lower", "upper")
      shares$val <- rep(seq(-700, 1200, 100), 2)
      shares$group <- c(rep("opp", 20), rep("gov", 20))
      
      row <- 0
      for (val in seq(-700, 1200, 100)) {
        row <- row + 1
        shares$share[row] <- length(which(mus[,"opp"]<val)) / nrow(mus)
      }
      for (val in seq(-700, 1200, 100)) {
        row <- row + 1
        shares$share[row] <- length(which(mus[,"gov"]<val)) / nrow(mus)
      }
      
      # get the 0.025 and 0.975 quantile values of the expected share
      lower_opp <- lower_gov <- upper_opp <- upper_gov <- rep(NA, 15)
      sharevar_opp <- sharevar_gov <- rep(NA, 1000)
      
      sim_iter <- 0
      for (val in seq(-700, 1200, 100)) {
        
        sim_iter <- sim_iter + 1
        
        for (i in 1:1000) {
          
          draws <- mvrnorm(1000, coef, vhat)
          
          mus[,1] <- draws %*% as.matrix(opp_setting)
          mus[,2] <- draws %*% as.matrix(gov_setting)
          
          sharevar_opp[i] <- length(which(mus[,"opp"]<val)) / nrow(mus)
          sharevar_gov[i] <- length(which(mus[,"gov"]<val)) / nrow(mus)
          
        }
        
        lower_opp[sim_iter] <- quantile(sort(sharevar_opp), 0.001)
        upper_opp[sim_iter] <- quantile(sort(sharevar_opp), 0.999)
        lower_gov[sim_iter] <- quantile(sort(sharevar_gov), 0.001)
        upper_gov[sim_iter] <- quantile(sort(sharevar_gov), 0.999)
        
        print(val)
        
      }
      
      shares$lower <- c(lower_opp, lower_gov)
      shares$upper <- c(upper_opp, upper_gov)
      
      tikz('cum_plot_gruene.tex', standAlone = TRUE, width=15, height=9)
      ggplot(shares, aes(x=val, y=share, group=group, color=group)) + 
        geom_vline(xintercept = 0, linetype="dashed", 
                   color = "black", size=1) +
        geom_point(size=4) +
        geom_errorbar(aes(ymin=lower, ymax=upper), width=1, size=1.5,
                      position=position_dodge(0.05)) +
        scale_colour_manual(values = c("black", "darkgrey"), name = "Policy Field", labels = c("Healthcare", "Environment")) +
        scale_fill_manual(values = c("black", "darkgrey"), name = "Policy Field", labels = c("Healthcare", "Environment")) +
        scale_x_continuous(breaks=seq(-700, 2100, 300)) +
        theme_bw() +
        #guides(fill = guide_legend(reverse = TRUE), color = guide_legend(reverse = TRUE)) + 
        theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
              axis.line = element_line(colour = "black"),  panel.border = element_blank(), 
              axis.text=element_text(size=25), axis.title=element_text(size=25),
              legend.text=element_text(size=25), legend.title=element_text(size=25)) +
        xlab("Expected Constructiveness") + ylab("Cumulative Share of Expected Constructiveness")
      dev.off()
      tools::texi2dvi('cum_plot_gruene.tex',pdf=T)
      system(paste(getOption('pdfviewer'),'cum_plot_gruene.pdf'))
      
log_close()